/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.admin.entity.MaintainContractType;
import org.springblade.modules.admin.vo.MaintainContractTypeVO;
import org.springblade.modules.admin.mapper.MaintainContractTypeMapper;
import org.springblade.modules.admin.service.IMaintainContractTypeService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 合同保养类型 服务实现类
 *
 * @author ysq
 * @since 2024-03-08
 */
@Service
public class MaintainContractTypeServiceImpl extends BaseServiceImpl<MaintainContractTypeMapper, MaintainContractType> implements IMaintainContractTypeService {

	@Override
	public IPage<MaintainContractTypeVO> selectMaintainContractTypePage(IPage<MaintainContractTypeVO> page, MaintainContractTypeVO maintainContractType) {
		return page.setRecords(baseMapper.selectMaintainContractTypePage(page, maintainContractType));
	}

	@Override
	public List<MaintainContractType> contractTypeList(List<MaintainContractType> list) {
		if (list.size() == 0) {
			list = this.list(Wrappers.<MaintainContractType>lambdaQuery().orderByDesc(MaintainContractType::getCreateTime));
		}
		//封装数据,因为数据很少变动,程序写死名称即可

		//1:部分配件2:全部配件3:不包配件
		Integer[] changeTypes = {0, 1, 2, 3};

		//1:清单内更换2:清单内不更换
		Integer[] changeRestrictTypes = {0, 1, 2};

		//配件金额类型1:以内更换2:以上不更换
		Integer[] partPricesTypes = {0, 1, 2};

		//1:清单内更换2:清单内不更换
		Integer[] changeListTypes = {0, 1, 2};

		for (MaintainContractType maintainContractType : list) {
			String name = maintainContractType.getName();
			if (maintainContractType.getChangeType() != null) {
				if (maintainContractType.getChangeType().equals(changeTypes[1])) {
					name = name + "(部分配件)";
				} else if (maintainContractType.getChangeType().equals(changeTypes[2])) {
					name = name + "(全部配件)";
				} else if (maintainContractType.getChangeType().equals(changeTypes[3])) {
					name = name + "(不包配件)";
				}
			}

			if (maintainContractType.getChangeRestrictType() != null && maintainContractType.getChangeType().equals(changeTypes[1])) {
				//配件更换限制--金额
				if (maintainContractType.getChangeRestrictType().equals(changeRestrictTypes[1])) {
					if (maintainContractType.getPartPricesType().equals(partPricesTypes[1])) {
						name = name + " 限制" + maintainContractType.getPartPrices() + "以内-更換";
					} else {
						name = name + " 限制" + maintainContractType.getPartPrices() + "以上-不更換";
					}
				} else {
					if (maintainContractType.getChangeListType().equals(changeListTypes[1])) {
						name = name + " 配件清单内-更换";
					} else {
						name = name + " 配件清单内-不更换";
					}
				}
			}
			maintainContractType.setName(name);
		}
		return list;
	}

	@Override
	public void addTenantInit(String addTenantNo) {
		try {
			String tenantId = "592800";
			List<MaintainContractType> list1 = baseMapper.list(addTenantNo, "清包");
			if (list1.size() > 0) {
				return;
			}
			List<MaintainContractType> list2 = baseMapper.list(tenantId, null);
			list2.forEach(o -> {
				o.setTenantId(addTenantNo);
				o.setId(null);
			});
			this.saveBatch(list2);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public MaintainContractType getOneNotDelete(Long id) {
		return baseMapper.getOneNotDelete(id);
	}

}
